$(document)
		.ready(
				function() {
					var id = getQueryString('id');
					console.log(id);

					$
							.ajax({
								type : "post",
								url : "articleStudyOne",
								async : true,
								data : {
									"id" : id
								},
								dataType : 'json',
								success : function(data) {
									console.log(data)
									var articlePath = 'myAssets//img//article//';// 文章文件路径
									var facePath = 'myAssets//img//face//';// 头像文件路径
									var id = data.articleVo.id;// 文章id
									var type = data.articleVo.type;// 类型
									var title = data.articleVo.title;// 标题
									var content = data.articleVo.content;// 内容
									var date = data.articleVo.date;// 发布时间
									var userName = data.articleVo.user.name;// 发布文章的user
									var userMyFile = data.articleVo.user.myFile.name;// 发布文章的user
									var goodArticle = data.articleVo.goodArticle;// 点赞user
																				// 集合

									var newdate = date.substring(0, 10);// 截取出
																		// 发布日期
																		// 取出掉时间
									// 分享
									if (type == 'fenxiang') {
										type = '<span class="am-badge am-badge-fenxiang am-text-lg">分享</span>'
									}
									// 教学
									if (type == 'jiaoxue') {
										type = '<span class="am-badge am-badge-jiaoxue am-text-lg">教学</span>'
									}
									// 提问
									if (type == 'tiwen') {
										type = '<span class="am-badge am-badge-tiwen am-text-lg">提问</span>'
									}
									// 趣闻
									if (type == 'quwen') {
										type = '<span class="am-badge am-badge-quwen  am-text-lg">趣闻</span>'
									
									}

									$("#user-name").html('<a href="heUser?id='+data.articleVo.user.id+'">'+userName+'</a>');
									$("#user-myFile").html(
											'<a href="heUser?id='+data.articleVo.user.id+'"><img src="' + facePath
													+ '//' + userMyFile
													+ '" alt="" class=""></a>');
									$("#title").html(title + ' ' + type);
									$("#date").html('发表于:' + ' ' + newdate);
									$("#content").html(content);

									var myFiles = data.articleVo.myFiles;// 文件
									for (var i = 0; i < myFiles.length; i++) {
										var myFilesname = myFiles[i].name;

										$("#articel-myflie")
												.append(
														'<img src="'
																+ articlePath
																+ myFilesname
																+ '" style="margin: 2px 2px 2px 2px"">');
									}
									var discusses = data.articleVo.discusses;
									if(discusses.length==0){
										$("#discusses-type").html('暂时无人评论');
									}
									if(discusses.length!=0){
										$("#discusses-type").html('精彩评论:  '+discusses.length+'条');
									}
									for (var x = 0; x < discusses.length; x++) {
										var userid = discusses[x].thisUser.id;
										var userName = discusses[x].thisUser.name;
										var discussesid = discusses[x].id;
										var dateProbably = getDateDiff(discusses[x].date
												.replace("T", " "));
										var content = discusses[x].content;
										var usetFile = facePath
												+ discusses[x].thisUser.myFile.name;
										var goodLength = 0;//被评论的数量
										var goalDiscussHashSetlength = discusses[x].goalDiscussHashSet.length;
										if (goodLength == 0) {
											goodLength = '';
										}
										if (goalDiscussHashSetlength == 0) {
											goalDiscussHashSetlength = '';
										}
										if (x == 0) {
											$("#discusses-list")
													.append(
															'	<div class="pet_comment_list_block"><a href="heUser?id='
																	+ userid
																	+ '"><div class="pet_comment_list_block_l"><img src="'
																	+ usetFile
																	+ '" alt=""></div>	<a /><div class="pet_comment_list_block_r"><a href="heUser?id='
																	+ userid
																	+ '"><div class="pet_comment_list_block_r_info">'
																	+ userName
																	+ '</div>	<a /><div class="pet_comment_list_block_r_text">'
																	+ content
																	+ '</div><div class="pet_comment_list_block_r_bottom">	<div class="pet_comment_list_bottom_info_l"style="color: #888888;">'
																	+ dateProbably
																	+ '</div>	<div class="pet_comment_list_bottom_info_r"><a href="###"> <span><i class="iconfont"><img src="myAssets/img/16-16/赞.png"></i>'
																	+ goodLength
																	+ '	</span>	</a> <a href="goalDiscussHashList?id='
																	+ discussesid
																	+ '"><span>'
																	+ goalDiscussHashSetlength
																	+ ' 回复</span></a>	</div>	</div>	</div>	</div>');
										} else {
											$("#discusses-list")
													.append(
															'<div class="pet_comment_list_block"><a href="heUser?id='
																	+ userid
																	+ '"><div class="pet_comment_list_block_l">	<img src="'
																	+ usetFile
																	+ '" alt="">	</div>	</a>	<div class="pet_comment_list_block_r">	<a href="heUser?id='
																	+ userid
																	+ '"><div class="pet_comment_list_block_r_info" style="color: #888888;">'
																	+ userName
																	+ '</div></a>	<div class="pet_comment_list_block_r_text">'
																	+ content
																	+ '</div><div class="pet_comment_list_block_r_bottom"><div class="pet_comment_list_bottom_info_l">'
																	+ dateProbably
																	+ '</div>	<div class="pet_comment_list_bottom_info_r">	<a href="###"> <span><i class="iconfont"><img src="myAssets/img/16-16/赞.png"></i>	'
																	+ goodLength
																	+ '</span>	</a> <a href="goalDiscussHashList?id='
																	+ discussesid
																	+ '"> <span>'
																	+ goalDiscussHashSetlength
																	+ ' 回复</span></a>	</div>	</div>	</div>	</div>');
										}

										if (x == 4) {
											$("#if-list-discusses").html('<hr><div class="am_news_load"><span><a href="discussListOfArticle?id='+data.id+'"> 查看更多评论</a></span></div>');
											break;
										}
									 }

								},
								error : function() {
								}
							});

				})
// 获取url中的参数
function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); // 匹配目标参数
	var result = window.location.search.substr(1).match(reg); // 对querystring匹配目标参数
	if (result != null) {
		return decodeURIComponent(result[2]);
	} else {
		return null;
	}
}

// 发布时间'2017-03-26T09:29:34' 返回"几天前"
function getDateDiff(dateTimeStamp) {
	dateTimeStamp = Date.parse(dateTimeStamp.replace(/-/gi, "/"));
	var minute = 1000 * 60;
	var hour = minute * 60;
	var day = hour * 24;
	var halfamonth = day * 15;
	var month = day * 30;
	var now = new Date().getTime();
	var diffValue = now - dateTimeStamp;
	if (diffValue < 0) {
		return;
	}
	var monthC = diffValue / month;
	var weekC = diffValue / (7 * day);
	var dayC = diffValue / day;
	var hourC = diffValue / hour;
	var minC = diffValue / minute;
	if (monthC >= 1) {
		result = "" + parseInt(monthC) + "月前";
	} else if (weekC >= 1) {
		result = "" + parseInt(weekC) + "周前";
	} else if (dayC >= 1) {
		result = "" + parseInt(dayC) + "天前";
	} else if (hourC >= 1) {
		result = "" + parseInt(hourC) + "小时前";
	} else if (minC >= 1) {
		result = "" + parseInt(minC) + "分钟前";
	} else
		result = "刚刚";
	return result;
}

$('#but-discuss').click(function() {
	var id = getQueryString('id');
	var content = $.trim($('#content-discuss').val());
	$.ajax({
		type : "post",
		async : true,
		url : "discussAddOfArticle",
		data : {
			'id' : id,
			'content' : content,
		},
		dataType : 'json',
		success : function(data) {
			console.log(data)
			var result = data.resultJson;
			if (result == '1') {
				window.location.href = "articleQuwenOne?id=" + data.id + "";
			} else {

			}

		},
		error : function(data) {
		}
	});

})
